package logic

import (
	"context"
	// "fmt"
	"strings"

	"mall/service/user/api/internal/svc"
	"mall/service/user/api/internal/types"

	// "github.com/sirupsen/logrus"
	"github.com/zeromicro/go-zero/core/logx"
)

type ChatLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewChatLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ChatLogic {
	return &ChatLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *ChatLogic) Chat(req *types.ChatReq) (*types.BaseMsgResp, error) {
	// logrus.Info(fmt.Sprintf("Chat : %+v", req))
	if req.Content != nil && len(strings.TrimSpace(*req.Content)) > 0 {
		var userId string
		if req.UserId != nil && len(*req.UserId) > 0 {
			userId = *req.UserId
		}
		if err := l.svcCtx.Hub.SendMessage([]byte(strings.TrimSpace(*req.Content)), userId); err != nil {
			logx.Errorw("Chat err", logx.Field("detail", err.Error()))
		}
	}

	return &types.BaseMsgResp{}, nil
}
